/*
 * Copyright (c) 2009 by Vsevolod Velichko <torkvemada@nigma.ru>
 *
 ***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************
*/

#include "indigo.h"

/**
 * @brief get version
 *
 * @return string - Cano version
 */
PHP_METHOD(PHP_CANO_CLASS_NAME, getVersion)
{
	RETURN_STRING((char*)canoGetVersion(), 1);
}

PHP_METHOD(PHP_CANO_CLASS_NAME, getCanonicalSmiles)
{
	char *molecule, *options = 0;
	long molecule_len, options_len = 0;
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, (char*)"s|s", &molecule, &molecule_len, &options, &options_len) == FAILURE)
	{
		RETURN_NULL();
	}
	if (canoSetMolecule(molecule) == 0)
	{
		php_error_docref(NULL TSRMLS_CC, E_NOTICE, "couldn't set molecule: %s", canoGetLastError());
		RETURN_NULL();
	}
	if (canoSetOptions(options == 0 ? "" : options) == 0)
	{
		php_error_docref(NULL TSRMLS_CC, E_NOTICE, "couldn't set options: %s", canoGetLastError());
		RETURN_NULL();
	}
	const char *canonicalSmiles;
	if (canoGetCanonicalSmiles(&canonicalSmiles) == 0)
	{
		php_error_docref(NULL TSRMLS_CC, E_NOTICE, "couldn't get canonical SMILES: %s", canoGetLastError());
		RETURN_NULL();
	}
	RETURN_STRING((char*)canonicalSmiles, 1);
}

PHP_METHOD(PHP_CANO_CLASS_NAME, getInChI)
{
	char *molecule, *options = 0;
	long molecule_len, options_len = 0;
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, (char*)"s|s", &molecule, &molecule_len, &options, &options_len) == FAILURE)
	{
		RETURN_NULL();
	}
	if (canoSetMolecule(molecule) == 0)
	{
		php_error_docref(NULL TSRMLS_CC, E_NOTICE, "couldn't set molecule: %s", canoGetLastError());
		RETURN_NULL();
	}
	if (canoSetOptions(options == 0 ? "" : options) == 0)
	{
		php_error_docref(NULL TSRMLS_CC, E_NOTICE, "couldn't set options: %s", canoGetLastError());
		RETURN_NULL();
	}
	const char *InChI;
	if (canoGetLayeredCode(&InChI) == 0)
	{
		php_error_docref(NULL TSRMLS_CC, E_NOTICE, "couldn't get InChI: %s", canoGetLastError());
		RETURN_NULL();
	}
	RETURN_STRING((char*)InChI, 1);
}

